﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class EditService : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] == null)
        {
            Response.Redirect("ViewService.aspx");
        }
        if (!DALService.CheckService(Request.QueryString["id"]))
        {
            Response.Redirect("ViewService.aspx");
        }
    }
    protected void frmEditService_ItemCommand(object sender, FormViewCommandEventArgs e)
    {
        if (e.CommandName=="Edit")
        {
            frmEditService.DefaultMode = FormViewMode.Edit;
        }
        if (e.CommandName=="Update")
        {
            SubmitInformation();
            frmEditService.DefaultMode = FormViewMode.ReadOnly;
        }
        if (e.CommandName=="Cancel")
        {
            frmEditService.DefaultMode = FormViewMode.ReadOnly;
        }
    }

    private string getFileFromFileUpload()
    {
        string fileUpload="";
        string str = "";
        try
        {
            FileUpload upload = (frmEditService.FindControl("ImageServiceUpload") as FileUpload);

            if (upload.HasFile && upload.PostedFile.ContentLength < 2097152)
            {
                str += "Uploading file: " + upload.FileName;

                upload.SaveAs(MapPath("~/Images/Service/" + upload.FileName));
                str += "<br/>Saved As: " + upload.PostedFile.FileName;
                str += "<br/>File Type: " + upload.PostedFile.ContentType;
                str += "<br/>File Length (bytes): " + upload.PostedFile.ContentLength;
                str += "<br/>PostedFile File Name: " + upload.PostedFile.FileName;
                fileUpload=upload.PostedFile.FileName;
            }
            else if (upload.HasFile && upload.PostedFile.ContentLength > 2097152)
            {
                str = "File upload must be smaller than 2 mb";
            }
            else
            {
                str = "No file uploaded.";
            }
          
           
        }
        catch (Exception ex)
        {

            str += "<br/><b>Error</b><br/>Unable to Update "+ ex.Message;
        }
        lblMessage.Text = str;
        return fileUpload;      
    }

    private void SubmitInformation()
    {
        try
        {
            string imageFile = getFileFromFileUpload();
            if (imageFile == "")
            {
                imageFile = (frmEditService.FindControl("lblImageUpload") as Label).Text;
            }
            string serviceName = (frmEditService.FindControl("txtServiceName") as TextBox).Text;
            string serviceDescription = (frmEditService.FindControl("txtDescription") as TextBox).Text;

            if (DALService.UpdateService(serviceName, imageFile, serviceDescription, Request.QueryString["id"].ToString()))
            {
                lblMessage.Text = "<br/> <b>Update Successful</b>";
            }
            else
            {
                lblMessage.Text = "<br/> <b>Update Fail</b>";
            }
            (frmEditService.FindControl("lblImageUpload") as Label).Text = imageFile;
        }
        catch
        {
            lblMessage.Text = "<br/><b>Error</b><br/>Unable to Update ";
        }
    }
    
}
